//
// Created by hjie on 23-7-8.
//
#include <cstring>
#include <string>
#include "./../base/link.h"

void ProducerLinkList(LinkList & link_list, int number)
{
    srand(time(NULL));

    link_list.head_node = new LinkNode();
    link_list.tail_node = link_list.head_node;

    link_list.head_node->data = rand() % 50;
    link_list.head_node->next_node = nullptr;
    link_list.head_node->prev_node = nullptr;

    for (int index = 0; index < number - 1; index++)
    {
        int data = rand() % 50;
        LinkNode * node = new LinkNode();
        node->data = data;
        node->next_node = nullptr;
        node->prev_node = nullptr;

        link_list.tail_node->next_node = node;
        node->prev_node = link_list.tail_node;

        link_list.tail_node = node;
    }
    printf("producer data : ");
    LinkNode * ptr = link_list.head_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");
}
// swap two node
// one by one
// example: first node swap with second node
// 3'st swap with 4'st node
void SwapNode()
{
    int number = 4;
    LinkList list{};
    ProducerLinkList(list, number);

    LinkNode * node = list.head_node;
    if (node == nullptr)
    {
        return;
    }
    LinkNode * node_next = node->next_node;
    if (node_next == nullptr)
    {
        return;
    }
    list.head_node;
    LinkNode * prev_node = node->prev_node;
    while(node && node_next)
    {
        LinkNode * ptr = node;
        ptr->next_node = node_next->next_node;
        node_next->next_node = ptr;
        if (prev_node)
        {
            prev_node->next_node = node_next;
        }
        prev_node = node;
        node_next = node->next_node;
        if (node_next)
        {
            node = node_next;
            node_next = node_next->next_node;
        }
    }
    printf("result data : ");
    LinkNode * ptr = list.head_node;
    while (ptr)
    {
        printf(" %d ", ptr->data);
        ptr = ptr->next_node;
    }
    printf("\n");
}

int main(int argc, const char * argv[])
{
    SwapNode();
    return 0;
}
